package com.drop.dao.impl;

import com.drop.dao.CommentDao;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.Date;
import com.drop.dao.CommentDao;
import com.drop.entity.Comment;
@Repository
public class CommentDaoImpl implements CommentDao {
	
	private Log log = LogFactory.getLog(this.getClass());

	private SessionFactory sessionFactory;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public Session getSession(){
		return sessionFactory.getCurrentSession();
	}


	@Override
	public Boolean saveComment(String[] comment, int orderid, String uid,
			String[] gids) {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		System.out.println("当前时间：" + sdf.format(d));
		String sql = "INSERT INTO `comment` (userId, goodsId, content, commentTime, commentImg , orderId)  " +
				"VALUES ";
		for(int i=0;i<comment.length;i++){
			if(i==0)
				sql+="('"+uid+"','"+gids[i]+"','"+comment[i]+"','"+sdf.format(d)+"','','"+orderid+"') ";
			else
				sql+=",('"+uid+"','"+gids[i]+"','"+comment[i]+"','"+sdf.format(d)+"','','"+orderid+"') ";
			
		}
		
		log.info(sql);
		return getSession().createSQLQuery(sql).executeUpdate() > 0 ;

		
	}

	@Override
	public List<Comment> getComment(int userid) {
		String hql="from Comment c where c.user.userId=?";
		return (List<Comment>)getSession().createQuery(hql).setParameter(0,userid).list();
	}

}
